import '@/js/layui_exts/admin.js'

export function init() {
    layui.use(() => {
        const $ = layui.jquery, admin = layui.admin, form = layui.form, table = layui.table, treeTable = layui.treeTable

        let amdType;
        const amdRole = role => {
            amdType = role ? 'modify' : 'add'
            layer.open({
                type: 1,
                title: role ? ('<i class="layui-icon-edit layui-icon"></i>&nbsp;编辑角色【' + role.name + '】') : ('<i class="layui-icon-add-circle layui-icon"></i>&nbsp;添加角色'),
                offset: role ? 'r' : 'l',
                anim: role ? 'slideLeft' : 'slideRight',
                shade: 0.3,
                area: ['500px', '100%'],
                shadeClose: true,
                scrollbar: false,
                id: 'amd-role-id',
                content: $('#amd-role-content')
            })

            form.val('amd-role-form-filter', role ?? {id: '', name: '', code: '', sort: '', desz: ''})
        }
        const rmRole = role => admin.confirm('确定删除【' + role.name + '】吗？', () => admin.postM('sys/role/remove', {id: role.id}, () => table.reload('role-table')), '删除提示')

        const grantRole = role => {
            layer.open({
                type: 1,
                title: '<i class="layui-icon-auz layui-icon"></i>&nbsp;角色绑定权限【' + role.name + '】',
                offset: 'r',
                anim: 'slideLeft',
                shade: 0.3,
                area: ['500px', '100%'],
                shadeClose: true,
                scrollbar: false,
                id: 'role-bind-auth-id',
                content: $('#role-auth-content')
            })

            treeTable.render({
                elem: '#role-auth-table',
                url: '/api/sys/role_auth/treetable/' + role.id,
                headers: {
                    'Authorization': admin.getToken()
                },
                tree: {
                    customName: {
                        name: 'title'
                    },
                    view: {
                        showIcon: false,
                        expandAllDefault: true
                    },
                    data: {
                        cascade: 'none'
                    }
                },
                parseData: ({code, msg, data}) => {
                    let d = JSON.parse(admin.decrypt(data))
                    return {
                        "code": code === 200 ? 0 : code,
                        "msg": msg,
                        "count": d.length,
                        "data": d
                    }
                },
                cols: [[{
                    type: 'checkbox'
                }, {
                    type: 'numbers',
                    title: '序号'
                }, {
                    field: 'title',
                    title: '名称',
                    templet: d => '<span style="color:' + (d.type === 'btn' ? '#16b777' : d.type === 'api' ? '#1e9fff' : '') + '">' + d.title + '</span>'
                }, {
                    field: "desz",
                    title: "说明"
                }]],
                page: false
            })

            treeTable.on('checkbox(role-auth-table)', ({checked, type, data}) => admin.postM('sys/role_auth/grant', {
                rid: role.id,
                checked,
                type,
                aid: data?.id
            }))
        }

        table.render({
            elem: '#role-table',
            url: '/api/sys/role/list',
            headers: {
                'Authorization': admin.getToken()
            },
            parseData: ({code, msg, data}) => {
                let d = JSON.parse(admin.decrypt(data))
                return {
                    "code": code === 200 ? 0 : code,
                    "msg": msg,
                    "count": d.length,
                    "data": d
                }
            },
            toolbar: '#role-table-toolbar',
            cols: [[{
                type: 'numbers',
                title: '序号'
            }, {
                field: 'name',
                title: '名称'
            }, {
                field: 'code',
                align: 'center',
                title: '编码'
            }, {
                field: 'sort',
                align: 'center',
                width: 100,
                title: '序号'
            }, {
                field: 'desz',
                title: '说明'
            }, {
                title: '操作',
                align: 'center',
                width: 300,
                toolbar: '#role-table-tools'
            }]],
            done: () => admin.perm()
        })

        table.on("tool(role-table)", ({event, data}) => {
            switch (event) {
                case 'md':
                    amdRole(data)
                    break;

                case 'rm':
                    rmRole(data)
                    break;

                case 'grant':
                    grantRole(data)
                    break;
            }
        })

        table.on("toolbar(role-table)", ({event}) => {
            switch (event) {
                case 'add':
                    amdRole()
                    break;
            }
        })

        form.on('submit(submit-amd-role-form)', ({field}) => {
            admin.postM('sys/role/' + amdType, field, () => {
                layer.closeAll('page')
                table.reload('role-table')
            })
            return false
        })
    })

    return {};
}
